package com.polaris.live.adapter.live

import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.polaris.live.adapter.home.FriendShipAdapter
import com.polaris.live.common.adapter.BaseAdapter
import com.polaris.live.common.ext.dp
import com.polaris.live.constant.LiveConst
import com.polaris.live.databinding.AdapterFriendShipBinding
import com.polaris.live.databinding.AdapterHomeLiveList2Binding
import com.polaris.live.databinding.LayoutLiveFollowRoomNoDataBinding
import com.polaris.live.resp.back_resp.DisplayUserBean
import com.polaris.live.utils.CommonUtils.toBoolean
import com.polaris.live.utils.image.ImageUtils
import com.polaris.live.utils.image.loadWejoyImage
import com.polaris.live.widget.refresh.BaseBindingViewHolder

/**
 * HomePartyAdapter
 *
 * @author Created by 半仙 on 2024/3/5/005 11:41
 */
class HomeFollowAdapter : BaseAdapter<Any, BaseViewHolder>(0) {

    override fun getDefItemViewType(position: Int): Int {
        return when (val item = data[position]) {
            is DisplayUserBean -> TYPE_NORMAL
            is String -> TYPE_DATA
            else -> throw IllegalArgumentException("Unknown item type: $item")
        }
    }

    override fun onCreateDefViewHolder(
        parent: ViewGroup,
        viewType: Int,
    ): BaseBindingViewHolder<*, *> {
        val layoutInflater = LayoutInflater.from(parent.context)
        return when (viewType) {
            TYPE_DATA -> DontHavaViewHolder(
                LayoutLiveFollowRoomNoDataBinding.inflate(layoutInflater, parent, false)
            )

            TYPE_NORMAL -> FriendShipAdapter.FriendShipViewHolder(
                AdapterFriendShipBinding.inflate(layoutInflater, parent, false)
            )

            else -> throw IllegalArgumentException("Invalid view type: $viewType")
        }
    }

    override fun convert(holder: BaseViewHolder, item: Any) {
        when (holder) {
            is FriendShipAdapter.FriendShipViewHolder -> if (item is DisplayUserBean) holder.bind(item)
            is DontHavaViewHolder -> if (item is String) holder.bind(item)
        }
    }

    class NormalManViewHolder(
        binding: AdapterHomeLiveList2Binding,
    ) : BaseBindingViewHolder<AdapterHomeLiveList2Binding, DisplayUserBean>(binding) {

        override fun bind(item: DisplayUserBean) {
            mBinding.apply {
                Glide.with(mBinding.root.context)
                    .loadWejoyImage(item.cover)
                    .transform(CenterCrop(), RoundedCorners(2.dp))
                    .into(userCover)

                nickName.text = item.base.nickname

                if (TextUtils.isEmpty(item.base.country)) {
                    userCountry.visibility = View.GONE
                } else {
                    userCountry.visibility = View.VISIBLE
                    userCountry.setCountry(item.base.country)
                }

                svState.setState(item.base, item)
                val user = item.base
                val partyGameType = user.partyGameType
                val showGameImg =
                    user.living?.toBoolean() == true && user.liveType == LiveConst.Type.PARTY_GAME_LIVE && partyGameType != null
                if (showGameImg) {
                    Glide.with(ivGameType).load(ImageUtils.imgPath(item.gameIcon)).into(ivGameType)
                    ivGameType.visibility = View.VISIBLE
                } else {
                    ivGameType.visibility = View.GONE
                }
                tvUserCount.text = item.stayUser.toString()
                headList.setList(item.siteUsers)
                widgetSubscript.setUserSymbol(item.symbol)
            }
        }
    }

    class DontHavaViewHolder(
        binding: LayoutLiveFollowRoomNoDataBinding,
    ) : BaseBindingViewHolder<LayoutLiveFollowRoomNoDataBinding, String>(binding) {

        override fun bind(item: String) {
            mBinding.apply {

            }
        }
    }

    companion object {

        const val TYPE_DATA = 0
        const val TYPE_NORMAL = 1
    }
}